﻿using CMS.Entities;
using CMS.Models;
using CMS.Windows;
using CommonServiceLocator;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using MahApps.Metro.Controls.Dialogs;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace CMS.ViewModel
{
    public class CargoViewModel : ViewModelBase
    {

        public CargoViewModel()
        {
            
        }

        private List<Models.Cargo> cargos = new List<Models.Cargo>();
        public List<Models.Cargo> Cargos
        {
            get { return cargos; }
            set
            {
                cargos = value;
                RaisePropertyChanged();
            }
        }

        //private List<Entities.Cargo> cargoExList = new List<Entities.Cargo>();
        ///// <summary>
        ///// 扩展物资
        ///// </summary>
        //public List<Entities.Cargo> CargoExList
        //{
        //    get { return cargoExList; }
        //    set
        //    {
        //        cargoExList = value;
        //        RaisePropertyChanged();
        //    }
        //}

        private List<CargoType> cargoTypes = new List<CargoType>();
        public List<CargoType> CargoTypes
        {
            get { return cargoTypes; }
            set
            {
                cargoTypes = value;
                RaisePropertyChanged();
            }
        }
        private List<UnitType> unitTypes = new List<UnitType>();
        public List<UnitType> UnitTypes
        {
            get { return unitTypes; }
            set
            {
                unitTypes = value;
                RaisePropertyChanged();
            }
        }




        public RelayCommand OpenAddCargoWindowCommand
        {
            get
            {
                return new RelayCommand(() =>
                {
                    var window = new AddCargoWindow();
                    window.ShowDialog();
                    //刷新数据
                    Cargos = new CargoProvider().Select();
                });
            }
        }


        /// <summary>
        /// 删除物资命令
        /// </summary>
        public RelayCommand<object> DeleteCargoCommand
        {
            get
            {
                return new RelayCommand<Object>((arg) =>
                {
                    if (MessageBox.Show("Delete?", "msg", MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.No)
                    {
                        return;
                    }
                    if (!(arg is Models.Cargo model)) return;
                    var count = new CargoProvider().Delete(model);
                    if (count > 0)
                    {
                        AppData.Instance.MainWindow.ShowMessageAsync("提示", "删除完成");
                    }
                    //刷新数据
                    Cargos = new CargoProvider().Select();
                });
            }
        }
        /// <summary>
        /// 编辑物资命令
        /// </summary>
        public RelayCommand<object> EditCargoCommand
        {
            get
            {
                return new RelayCommand<Object>((arg) =>
                {
                    if (!(arg is Models.Cargo model)) return;
                    model.TypeId = this.CargoTypes.FirstOrDefault(t => t.Name == model.TypeName).Id;
                    model.UnitId = this.UnitTypes.FirstOrDefault(t => t.Name == model.UnitName).Id;
                    var count = new CargoProvider().Update(model);
                    if (count > 0)
                    {
                        AppData.Instance.MainWindow.ShowMessageAsync("提示", "操作成功");
                    }
                    //刷新数据
                    Cargos = new CargoProvider().Select();
                });
            }
        }

        public RelayCommand<Cargo> OpenAddInputCargoWindowCommand
        {
            get
            {
                return new RelayCommand<Cargo>((cargo) =>
                {
                    if (cargo == null) return;
                    var inputCargoViewModel = ServiceLocator.Current.GetInstance<InputCargoViewModel>();
                    if (inputCargoViewModel == null) return;                    
                    inputCargoViewModel.Cargo = cargo;
                    var window = new InputCargoWindow();
                    window.ShowDialog();
                    //刷新数据
                    Cargos = new CargoProvider().Select();
                });
            }
        }

        /// <summary>
        /// 打开出库对话框
        /// </summary>
        public RelayCommand<Cargo> OpenAddOutputCargoWindowCommand
        {
            get
            {
                return new RelayCommand<Cargo>((cargo) =>
                {
                    var outputCargoViewModel = ServiceLocator.Current.GetInstance<OutputCargoViewModel>();
                    if (outputCargoViewModel == null) return;
                    if (cargo == null) return;
                    outputCargoViewModel.Cargo = cargo;
                    var window = new OutputCargoWindow();
                    window.ShowDialog();
                    //刷新数据
                    Cargos = new CargoProvider().Select();
                });
            }
        }

    }//public class CargoTypeViewModel : ViewModelBase  
}
